from remind.models import RemindModel, DoneListModel
from django.test import TestCase
from django.contrib.auth.models import User
from backend.models.login import GroupModel
import datetime
import time


class RemindModelTestCase(TestCase):
    def setUp(self):
        user = User.objects.create_user(username="zhangsan", password="pass123456", email="12345678@qq.com",
                                        first_name="张三")
        group = GroupModel.objects.create(name="test_group", owner=user)
        group.members.add(user)
        RemindModel.objects.create(owner=user, time=datetime.datetime.now(), notify_time=datetime.datetime.now(),
                                   event="吃饭", defer=8)
        remind = RemindModel.objects.get(defer=8)
        remind.groups.add(group)
        remind.save()
        self.remind = remind
        self.user = user
        self.group = group

    def test_create(self):

        remind = self.remind
        user = self.user
        group = self.group
        i = 5
        while i > 0 and not len(remind.groups.all()):
            print(remind.groups.all())
            time.sleep(2)
            i -= 1
            if i == 0:
                raise RuntimeError("提醒添加组失败")
        self.assertEqual(group, remind.groups.all()[0])
        i = 5
        while i > 0 and not DoneListModel.objects.filter(user=user, remind=remind).exists():
            time.sleep(2)
            i -= 1
            if i == 0:
                raise RuntimeError("自动创建对应提醒完成情况列表失败")
        done = DoneListModel.objects.get(user=user, remind=remind)
        self.assertEqual(remind, done.remind)
